package com.javandad.app.client.cells;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.javandad.app.shared.entity.User;
import com.javandad.app.shared.entity.UserGroup;
import com.javandad.app.shared.entity.project.Project;
import com.javandad.app.shared.entity.search.SearchResult;

public class SearchResultCell extends AbstractCell<SearchResult> {

	@Override
	public void render(com.google.gwt.cell.client.Cell.Context context, SearchResult value, SafeHtmlBuilder sb) {
		switch (value.getSearchResultType()) {
		case User:
			User u = (User) value;
			renderUser(context, u, sb);
			break;
		case Group:
			UserGroup g = (UserGroup) value;
			renderGroup(context, g, sb);
			break;
		case Project:
			Project p = (Project) value;
			renderProject(context, p, sb);
			break;
		}
	}

	private static void renderUser(com.google.gwt.cell.client.Cell.Context context, User u, SafeHtmlBuilder sb) {
		sb.appendHtmlConstant("<p><table align='left' border='0' bgcolor='edecf6' width='100%' height='100%'><tr aligh='left'><td><img width='64' height='64' src='" + u.getProfileFodoURL()
				+ "'></img></td>");
		sb.appendHtmlConstant("<td width='100%' align='left'><table width='100%' height='100%'><tr><td><b>");
		sb.appendEscaped(u.toString() + " ( " + u.getUsername() + " ) ");
		sb.appendHtmlConstant("</b></td></tr>");
		sb.appendHtmlConstant("<tr><td>");
		sb.appendEscaped(u.getEmail());
		sb.appendHtmlConstant("</td></tr>");
		sb.appendHtmlConstant("<tr><td>");
		String city = u.getCity();
		String country = u.getCountry();

		if (city != null)
			sb.appendEscaped(city);

		if ((city != null && !city.equals("")) && (country != null && !country.equals("")))
			sb.appendEscaped(", ");

		if (country != null)
			sb.appendEscaped(country);

		sb.appendHtmlConstant("</td></tr></table></td></tr></table></p>");
	}

	private static void renderGroup(com.google.gwt.cell.client.Cell.Context context, UserGroup g, SafeHtmlBuilder sb) {

		sb.appendHtmlConstant("<p><table align='left' border='0' bgcolor='edecf6' width='100%' height='100%'><tr aligh='left'><td><img width='64' height='64' src='" + g.getGroupFodoURL()
				+ "'></img></td>");
		sb.appendHtmlConstant("<td width='100%' align='left'><table width='100%' height='100%'><tr><td><b>");
		sb.appendEscaped(g.getGroupURL());
		sb.appendHtmlConstant("</b></td></tr>");
		sb.appendHtmlConstant("<tr><td>");
		sb.appendEscaped("Created by " + g.getCreator());
		sb.appendHtmlConstant("</td></tr>");
		sb.appendHtmlConstant("<tr><td>");
		sb.appendEscaped("Since " + g.getDateOfEstablishment().toString());

		sb.appendHtmlConstant("</td></tr></table></td></tr></table></p>");

	}

	private static void renderProject(com.google.gwt.cell.client.Cell.Context context, Project p, SafeHtmlBuilder sb) {
		sb.appendHtmlConstant("<p><table align='left' border='0' bgcolor='edecf6' width='100%' height='100%'><tr aligh='left' height='64'><td width='64'><img width='64' height='64' src='"
				+ p.getProjectFodoURL() + "'></img></td>");
		sb.appendHtmlConstant("<td width='100%' align='left'><table width='100%' height='100%'><tr><td><b>");
		sb.appendEscaped(p.getName());
		sb.appendHtmlConstant("</b></td></tr>");
		sb.appendHtmlConstant("<tr><td>");
		sb.appendEscaped("Type : " + ((p.getProjectType() == 1) ? "Library" : "Application"));
		sb.appendHtmlConstant("</td></tr>");
		sb.appendHtmlConstant("<tr><td>");
		sb.appendEscaped("Since ");

		sb.appendHtmlConstant("</td></tr></table></td></tr></table></p>");
	}

}
